package com.minyifang.wcspf.work.ui.user.activities;

import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.minyifang.wcspf.R;
import com.minyifang.wcspf.common.base.BaseMvpActivity;
import com.minyifang.wcspf.common.md5.SafeUtils;
import com.minyifang.wcspf.common.utils.LogUtil;
import com.minyifang.wcspf.common.utils.ToastHelper;
import com.minyifang.wcspf.work.model.HttpRespond;
import com.minyifang.wcspf.work.ui.user.model.WalletDetailBean;
import com.minyifang.wcspf.work.ui.user.presenter.WalletDetailPresenter;
import com.minyifang.wcspf.work.ui.user.view.WalletDetailView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class WalletDetailActivity extends BaseMvpActivity<WalletDetailView, WalletDetailPresenter> implements WalletDetailView {

    @BindView(R.id.refreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    private List<WalletDetailBean> mList;
    private MyAdapter mAdapter;

    private int pageIndex = 0;

    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_wallet_detail;
    }

    @Override
    protected void initView() {

        mList = new ArrayList<>();
        mAdapter = new MyAdapter(R.layout.item_wallet_detail, mList);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));

        mRecyclerView.setAdapter(mAdapter);

        mRefreshLayout.setOnRefreshListener(refreshLayout -> {
            pageIndex = 0;
            mList.clear();
            mPresenter.getList(pageIndex);
        }).setOnLoadMoreListener(refreshLayout -> mPresenter.getList(pageIndex));

    }

    @Override
    protected void initData() {
        mPresenter.getList(pageIndex);

    }

    @Override
    protected WalletDetailPresenter createPresenter() {
        return new WalletDetailPresenter();
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        mRefreshLayout.finishRefresh().finishLoadMore();
        hideLoadingDialog();
    }

    @Override
    public void showBalanceDetailRes(HttpRespond<String> respond) {
        mRefreshLayout.finishRefresh().finishLoadMore();
        if (respond.result == 1) {
            if (respond.isaes == 1) {
                LogUtil.e("资金明细数据: " + SafeUtils.decrypt(respond.data));
                List<WalletDetailBean> list = new Gson().fromJson(SafeUtils.decrypt(respond.data), new TypeToken<List<WalletDetailBean>>() {
                }.getType());
                if (list.size() > 0) {
                    pageIndex++;
                }
                mList.addAll(list);
                mAdapter.notifyDataSetChanged();
            }
        } else
            ToastHelper.getInstance(this).showWarn(respond.message);

    }

    class MyAdapter extends BaseQuickAdapter<WalletDetailBean, BaseViewHolder> {
        public MyAdapter(int layoutResId, @Nullable List<WalletDetailBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, WalletDetailBean item) {
            helper.setText(R.id.item_content, item.getChangetype());
            helper.setText(R.id.item_date, item.getChangedatetime());
            helper.setText(R.id.item_money, item.getChange());
        }
    }
}

